<template>
	<view class="content">
		<view class="welcome-content">
			不良事件上报管理系统
		</view>
		<view class="form-content">
			<u--input v-model="form.username" prefixIcon="account" placeholder="账号" border="surround">

			</u--input>
			<u--input style="margin: 24rpx 0;" prefixIcon="lock" v-model="form.password" type="password"
				placeholder="密码" border="surround">
			</u--input>
			<view class="code-content">
				<u--input v-model="form.code" prefixIcon="chat" placeholder="验证码" border="surround">
					<u--image slot="suffix" aspectFit :showLoading="true" :src="codeUrl" width="100px" height="28px"
						@click="getCaptchaImage" mode="aspectFit"></u--image>
				</u--input>
			</view>
			<view class="form-bottom">
				<view></view>
				<view @click="toRegister">
					忘记密码？
				</view>
			</view>
			<u-button @click="login()" type="primary">登录</u-button>
		</view>
	</view>
</template>

<script>
	import {
		getCaptchaImage,
		login,
		getUserInfo
	} from '@/api/login'
	export default {
		data() {
			return {
				codeUrl: "https://uviewui.com/album/1.jpg",
				captchaEnabled: true,
				form: {
					username: 'admin',
					password: 'admin123'
				},
			}
		},
		onLoad() {
			this.getCaptchaImage()
		},
		methods: {
			//登录
			login() {
				uni.showLoading({
					title: '登录中...'
				})
				login(this.form).then(res => {
					uni.setStorageSync('token', res.data.token)
					this.getUserInfo()
				}).finally(() => {
					uni.hideLoading()
				})
			},
			//获取userInfo
			getUserInfo() {
				getUserInfo().then(res => {
					uni.setStorageSync('userInfo', res.data)
					this.initData()
					uni.reLaunch({
						url: '/pages/home/index'
					})
				})
			},
			initData() {
				this.form = {
					username: '',
					password: '',
				}
			},
			//获取图形验证码
			getCaptchaImage() {
				getCaptchaImage().then(res => {
					this.captchaEnabled = res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled;
					if (this.captchaEnabled) {
						this.codeUrl = "data:image/gif;base64," + res.data.img;
						this.form.uuid = res.data.uuid;
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		.welcome-content {
			font-size: 42rpx;
			padding: 260rpx 48rpx 0 48rpx;
			color: #333;
		}

		.logo {
			display: flex;
			justify-content: center;
			align-items: center;
		}

		.code-content {
			display: flex;
			justify-content: space-between;
		}

		.form-content {
			padding: 84rpx 48rpx;

			.form-bottom {
				display: flex;
				justify-content: space-between;
				align-items: center;
				font-size: 32rpx;
				margin: 48rpx 0;
				color: #82848a;
			}
		}
	}
</style>